package com.ithe.shopweb.controller;

import com.ithe.shopweb.common.Constants;
import com.ithe.shopweb.common.Result;
import com.ithe.shopweb.pojo.Customer;
import com.ithe.shopweb.pojo.Manager;
import com.ithe.shopweb.pojo.Offer;
import com.ithe.shopweb.pojo.resp.CustomerResp;
import com.ithe.shopweb.pojo.resp.ManageResp;
import com.ithe.shopweb.pojo.resp.OfferResp;
import com.ithe.shopweb.serve.ManageServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 日期： 2024/5/26 12:20
 */
@RestController
@RequestMapping("/manage")
public class ManageController implements Constants {

    @Autowired
    private ManageServe manageServe;

    @PostMapping("/queryState")
    public Result queryState(@RequestBody Offer offer) {
        String str = offer.getSearchStr();
        Integer pageNum = offer.getPageNum();
        Integer pageSize = offer.getPageSize();

        List<OfferResp> offerResp = manageServe.queryState(str, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = manageServe.queryState(str, 0, 999).size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", offerResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "暂无任何管理员数据");
        else
            return Result.success(offerList);
    }

    @PostMapping("/queryOffer")
    public Result queryOffer(@RequestBody Offer offer) {
        String str = offer.getSearchStr();
        Integer pageNum = offer.getPageNum();
        Integer pageSize = offer.getPageSize();

        List<OfferResp> offerResp = manageServe.queryOffer(str, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = manageServe.queryOffer(str, 0, 999).size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", offerResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "暂无任何管理员数据");
        else
            return Result.success(offerList);
    }

    @PostMapping("/deleteOffer")
    public Result deleteOffer(@RequestBody Offer offer) {
        String offerID = offer.getOfferID();
        Integer number = manageServe.deleteOffer(offerID);

        if(number == 1)
            return Result.success();
        else
            return Result.error(CODE_500, "删除失败");
    }

    @PostMapping("/queryCustomer")
    public Result queryCustomer(@RequestBody Customer customer) {
        String str = customer.getSearchStr();
        Integer pageNum = customer.getPageNum();
        Integer pageSize = customer.getPageSize();

        List<CustomerResp> customerResp = manageServe.queryCustomer(str, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = customerResp.size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", customerResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "暂无任何管理员数据");
        else
            return Result.success(offerList);
    }

    @PostMapping("/deleteCustomer")
    public Result deleteCustomer(@RequestBody Customer customer) {
        String userID = customer.getUserID();
        Integer number = manageServe.deleteCustomer(userID);

        if(number == 1)
            return Result.success();
        else
            return Result.error(CODE_500, "删除失败");
    }

    @PostMapping("/queryAdmin")
    public Result queryAdmin(@RequestBody Manager manager) {
        String str = manager.getSearchStr();
        Integer pageNum = manager.getPageNum();
        Integer pageSize = manager.getPageSize();

        List<ManageResp> manageResp = manageServe.queryAdmin (str, (pageNum-1)*pageSize, pageSize);
        Integer totalNum = manageResp.size();

        Map<String, Object> offerList = new HashMap<>();
        offerList.put("total", totalNum);
        offerList.put("data", manageResp);

        if(offerList.get("data") == null)
            return Result.error(CODE_400, "暂无任何管理员数据");
        else
            return Result.success(offerList);
    }

    @PostMapping("/deleteAdmin")
    public Result deleteAdmin(@RequestBody Manager manager) {
        String adminID = manager.getAdminID();
        Integer number = manageServe.deleteAdmin(adminID);

        if(number == 1)
            return Result.success();
        else
            return Result.error(CODE_500, "删除失败");
    }
}
